package com.zhongyouyun;


import com.linuxense.javadbf.DBFDataType;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFWriter;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.stream.Collectors;


public class test {

    public static void main(String[] args) throws Exception {


//        String urlString="";
//        URL url = new URL("http://127.0.0.1:8055/doc.html");
//        URLConnection connection = url.openConnection();
//        InputStream inputStream = connection.getInputStream();
//        System.out.println(connection.getContentEncoding());
//        // 字节流转化为字符流的缓冲流
//        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
//        FileWriter fileWriter = new FileWriter(System.currentTimeMillis() + ".html");
//        System.out.println("当前编码方式为:" + fileWriter.getEncoding());
//        String line = "";
//        while ((line = bufferedReader.readLine()) != null) {
//            System.out.println(line);
//            fileWriter.write(line);
//        }
//        fileWriter.close();
    }

    // 32个字符，用来表示32进制
    final static char[] digits = {
            '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'A', 'B',
            'C', 'D', 'E', 'F', 'G', 'H',
            'J', 'K', 'L', 'M', 'N', 'P',
            'Q', 'R', 'T', 'U', 'V', 'W',
            'X', 'Y', 'a', 'b', 'c', 'd',
            'e', 'f', 'g', 'h', 'j', 'k',
            'l', 'm', 'n', 'p', 'q', 'r',
            't', 'u', 'v', 'w', 'x', 'y'
    };

//    public static void main(String[] args) throws IOException {
////        ExcelReader reader = ExcelUtil.getReader("H:/三门峡市2023年市直机关公开遴选公务员报考系统(已审核).xls");
////        List<Map<String, Object>> readAll = reader.readAll();
////        System.out.println(readAll);
//        List<Map<String, Object>> readAll = new ArrayList<>();
//        Map<String, Object> map=new HashMap<>();
//        map.put("data",new Date());
//        readAll.add((map));
//        exportdbf("c:/temp/test01.dbf",readAll);
//    }

    /**
     * list 生成 dbf
     *
     * @param dbfname 文件 名
     * @param listdata  文件源数据
     *
     * @throws IOException
     */
    public static void exportdbf(String dbfname, List<Map<String, Object>> listdata) throws IOException {
        File file = new File(dbfname);
        if(!file.exists()){
            System.out.println("----------------");
            return ;
        }

        int i2 = 0;
        for(String key : listdata.get(0).keySet()) {
            i2++;
        }
        DBFField fields[] = new DBFField[i2];

        int i = 0;
        for(String key : listdata.get(0).keySet()) {
                fields[i] = new DBFField();
                fields[i].setName(key);
                fields[i].setType(DBFDataType.DATE);
                fields[i].setLength(8);
//            if ("XB".equals(key)){
//                fields[i] = new DBFField();
//                fields[i].setName(key);
//                fields[i].setType(DBFDataType.TIMESTAMP_DBASE7);
//                fields[i].setLength(10);
//            }else{
//                fields[i] = new DBFField();
//                fields[i].setName(key);
//                fields[i].setType(DBFDataType.CHARACTER);
//                fields[i].setLength(10);
//            }
//
//            i++;
        }

        FileOutputStream fos = new FileOutputStream(dbfname);
        DBFWriter writer = new DBFWriter(fos);
        writer.setFields(fields);
        for (int j = 0; j < listdata.size(); j++) {
//      HashMap<String,Object> m3 = listdata.get(j);
            Object rowData[] = new Object[1];
            int i1 = 0;
            for(String key : listdata.get(j).keySet()) {
                rowData[i1] = listdata.get(j).get(key);
                i1++;
            }
            writer.addRecord(rowData);
        }

        writer.write(fos);
        fos.close();
        System.out.println("dbf文件生成！");
    }


    /**
     * long类型转为32进制，指定了使用的字符，参考Long.toUnsignedString0
     *
     * @param val
     * @return
     */
    static String digits(long val) {
        // 32=2^5=二进制100000
        int shift = 12;
        // numberOfLeadingZeros 获取long值从高位连续为0的个数，比如val=0，则返回64
        // 此处mag=long值二进制减去高位0之后的长度
        System.out.println(Long.SIZE);
        System.out.println(Long.numberOfLeadingZeros(val));
        int mag = Long.SIZE - Long.numberOfLeadingZeros(val);
        System.out.println(mag);
        int len = Math.max(((mag + (shift - 1)) / shift), 1);
        System.out.println(len);
        char[] buf = new char[len];
        do {

            // &64相当于%63
            buf[--len] = digits[((int) val) & 59];
            System.out.println(buf[len]);
            val >>>= shift;

        } while (val != 0 && len > 0);
        return new String(buf);
    }
}